package com.qcby.lxt.framedemo.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcby.lxt.framedemo.entity.QuartzJob;
import com.qcby.lxt.framedemo.service.IJobService;
import com.qcby.lxt.framedemo.util.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @className: RestJobController
 * @description:
 * @author: lxt
 * @create: 2021-04-19 19:42
 **/
@RestController
@RequestMapping("rest/job")
@Slf4j
public class RestJobController {
    @Autowired
    private IJobService jobService;

    @PostMapping("/list/{currentPage}/{pageSize}")
    public AjaxResult list(@RequestBody QuartzJob quartz, @PathVariable Integer currentPage, @PathVariable Integer pageSize) {
        log.info("任务列表");
        IPage<QuartzJob> pageInfo = jobService.listQuartzJob(quartz.getJobName(), new Page<>(currentPage, pageSize));
        return AjaxResult.success(pageInfo);
    }

    @PostMapping("/add")
    public AjaxResult save(@RequestBody QuartzJob quartz) {
        log.info("新增任务");
        AjaxResult result = jobService.saveJob(quartz);
        return result;
    }

    @PostMapping("/trigger")
    public AjaxResult trigger(@RequestBody QuartzJob quartz) {
        log.info("触发任务");
        AjaxResult result = jobService.triggerJob(quartz.getJobName(), quartz.getJobGroup());
        return result;
    }
    @PostMapping("/pause")
    public AjaxResult pause(@RequestBody QuartzJob quartz) {
        log.info("停止任务");
        AjaxResult result = jobService.pauseJob(quartz.getJobName(), quartz.getJobGroup());
        return result;
    }
    @PostMapping("/resume")
    public AjaxResult resume(@RequestBody QuartzJob quartz) {
        log.info("恢复任务");
        AjaxResult result = jobService.resumeJob(quartz.getJobName(), quartz.getJobGroup());
        return result;
    }

    @PostMapping("/delete")
    public AjaxResult remove(@RequestBody QuartzJob quartz) {
        log.info("移除任务");
        AjaxResult result = jobService.removeJob(quartz.getJobName(), quartz.getJobGroup());
        return result;
    }
}
